#include "Image.hpp"
#include "Region.hpp"
Image::Image(){
}
Image::Image(IplImage *img){
	m_height=img->height;
	m_width=img->width;
	m_x=0;
	m_y=0;
	m_coul=0;
	m_image=new Pixel*[ m_height ];
	for (int i = 0 ; i < m_height ; i++){
		m_image[i]=new Pixel[m_width];
	}
	for (int i=0;i<img->height ;i++){
		for (int j=0;j<img->width;j++){
			CvScalar pixel = cvGet2D(img,i,j);
			int valeur = pixel.val[0];     
			m_image[i][j].NiveauDeGris=valeur;
		}
	}
}
Image::Image(int x,int y,int h,int w,Pixel** image):m_x(x),m_y(y),m_height(h),m_width(w),m_image(image){

}
void Image::split(vector <Image>&v){
	int newHeight1 = (m_height/2)+1;
	int newHeight2 = m_height%2==0? (m_height/2) : (m_height/2)+1;
	int newWidth1 = (m_width/2)+1;
	int newWidth2 = m_width%2==0? (m_width/2) : (m_width/2)+1;
	int newY = (m_height/2)+m_y;
	int newX= (m_width/2)+m_x;
        //division de l'image en quatre
	v.push_back(Image(m_x,m_y,newHeight1,newWidth1,m_image));
	v.push_back(Image(newX,m_y,newHeight1,newWidth2,m_image));
	v.push_back(Image(m_x,newY,newHeight2,newWidth1,m_image));
	v.push_back(Image(newX,newY,newHeight2,newWidth2,m_image));
}
bool Image::IsSmal(){
    return (m_height<Util::tailleMinimale || m_width<Util::tailleMinimale);
}
bool Image::IsHomogene(){
	vector<Pixel> extrait;
        //analyse de 80 pourcents des points de l'image
        /*for(int i=0;i<(m_height*m_width)/80;i++){
		int posy = rand()%(m_height);
		int posx = rand()%(m_width); 
		extrait.push_back(m_image[posy+m_y][m_x+posx]);
        }*/
        for (int i=0;i<m_height;i++){
		for (int j=0;j<m_width;j++){
			extrait.push_back(m_image[i+m_y][m_x+j]);
		}
        }
        //Calcull de l'ecart type
	double ecart = Util::CalculEcartType(extrait, m_moyenne);
        //retour vrai si inferieur a la valeur critique de l'ecart type
        if(ecart<Util::ecartType){
		return true;
	}
	return false;
}
bool Image::IsAdjacent(const Image &image){
    //definition de toutes les postions possibles des deux images
	if(m_x==image.getX()+image.getwidth()){
		if(!(m_y+m_height-1<image.getY() || image.getY()+image.getheight()-1<m_y)){
			return true;
		}
	}else if(m_x+m_width-1==image.getX()){
		if(!(m_y+m_height-1<image.getY() || image.getY()+image.getheight()-1<m_y)){
			return true;
		}
	}else if(m_y+m_height-1==image.getY()){
		if(!(m_x+m_width-1<image.getX() || image.getX()+image.getwidth()-1<m_x)){
			return true;
                    }
        }else if(m_y==image.getY()+image.getheight()-1){
			if(!(m_x+m_width-1<image.getX() || image.getX()+image.getwidth()-1<m_x)){
				return true;
			}
        }

	return false;
 }
 void Image::detruireImage(){
     //liberation de l'image stockee dans la pile
    for (int i = 0 ; i < m_height ; i++){
	    delete[] m_image[i];
    }
    delete[] m_image;
    //supression des regions
    RegionImage::getTotal()->clear();
 }
 bool Image::isRessemblant( const Image &image){
     double diff = Util::resemblance;
    if(m_moyenne<=image.getMoyenne()){
        //Comparaison des deux moyennes
	    return (image.getMoyenne()-m_moyenne)<diff;
    }else{
        //Comparaison des deux moyennes
	    return (m_moyenne-image.getMoyenne())<diff;
    }
 }
 double Image::getMoyenne() const{
	return m_moyenne;
 }
